package dnl.util.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class represents a general dialog that contains a column of labels
 * and a column of corresponding JTextFields, and a convenient, general, way
 * for retrieving the data in the fields.
 *
 * Description:
 * @author Daniel Or
 * @version 1.0
 */

public class GridDialog extends Dialog {

  JTextField[] flds;
  String[] labels;
  String[] defaultVals;
  JPanel main = new JPanel();

  // marks if user canceled dialog
  // (I prefered a boolean instead of adapter class)
  boolean canceled;

  /**
   * @param owner the owner of the dialog
   * @param labels a String array containing the labels to be put.
   * @param defaultVals the default values to be put in the text fields.
   */
  public GridDialog(Frame owner, String[] labels, String[] defaultVals) {
    super(owner, "New Connection", true);
    if( labels == null )
      throw new IllegalArgumentException("GridDialog(): param labels cannot be null");
    this.labels = labels;
    this.defaultVals = defaultVals;
    flds = new JTextField[labels.length];
    getContentPane().setLayout(new BorderLayout());
    main.setLayout(new GridLayout(labels.length, 2) );
    for (int i = 0; i < flds.length; i++) { // without name
      String s = "";
      if( defaultVals != null && defaultVals[i] != null)
        s = defaultVals[i];
      flds[i] = new JTextField(s);
      if( labels[i] == null )
        throw new IllegalArgumentException("GridDialog(): values in labels cannot be null");
      flds[i].setName( labels[i] );
      main.add(new JLabel(labels[i]));
      main.add(flds[i]);
    }

    getContentPane().add(main);
    final JButton okbut = new JButton("OK");
    final JButton cancelbut = new JButton("Cancel");
    JPanel jpan = new JPanel();
    jpan.add(okbut);
    jpan.add(cancelbut);
    ActionListener al = new ActionListener(){
      @Override
	public void actionPerformed(ActionEvent ae){
        if( ae.getSource() == cancelbut)
          canceled = true;
        else
          canceled = false;
        setVisible(false);
      }
    };
    getContentPane().add("South", jpan);
    okbut.addActionListener(al);
    cancelbut.addActionListener(al);
    pack();
  }

  /**
   * @returns the value in the JTextField that corresponds
   *  to the label which name is <code>fieldName</code>
   *  returns null if user pressed on "Cancel" button
   */
  public String getValue(String fieldName){
    if(canceled) return null;
    for (int i = 0; i < flds.length; i++) {
      if( flds[i].getName().equals(fieldName) )
        return flds[i].getText();
    }
    return null;
  }

  public void setValue(String fieldName, String value){
	  for (int i = 0; i < flds.length; i++) {
		  if( flds[i].getName().equals(fieldName) )
			  flds[i].setText(value);
	  }
  }

  public String[] getValues(){
    if(canceled) return null;
    String[] vals = new String[labels.length];
    for (int i = 0; i < labels.length; i++) {
      vals[i] = flds[i].getText();
    }

    return vals;
  }
}